home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / AppKiller 1.2.1 / src / appkiller.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-04  |  4.8 KB  |  196 lines  |  [TEXT/CWIE]

  1. /*************
  2.   appkiller.c
  3.   
  4.   This file contains main() which in turn initializes all the different
  5.   parts of the program and Operating System.  The main event loop
  6.   is in main().
  7. *************/
  8.  
  9. #ifndef THINK_C
  10.  #include <AppleEvents.h>
  11.  #include <Desk.h>
  12.  #include <Dialogs.h>
  13.  #include <Events.h>
  14.  #include <Fonts.h>
  15.  #include <GestaltEqu.h>
  16.  #include <Menus.h>
  17.  #include <OSEvents.h>
  18.  #include <QuickDraw.h>
  19.  #include <ToolUtils.h>
  20.  #include <Types.h>
  21.  #include <Windows.h>
  22. #else
  23.  #include "ak_headers"
  24. #endif
  25. #include "aevent.h"
  26. #include "appkiller.h"
  27. #include "ak_ppc.h"
  28. #include "trapavail.h"
  29.  
  30. static void Init(void);
  31. static void Check4Sys7(void);
  32. static void SetupMenus(void);
  33. static void DoEvent(void);
  34. static void DoMouseDown(void);
  35. static void DoKeyDown(void);
  36. static void DoCommand(long);
  37. static void DoHLEvent(EventRecord *);
  38.        void DoAlert(Str255, Str255, Str255, Str255);
  39. static void DoAbout(void);
  40.  
  41. DialogPtr        gAboutDlog;                    /* About box...        */
  42. WindowPtr        gWhichWindow;                /* window of event     */
  43. MenuHandle        gAppleM;                    /* handles to menus    */
  44. EventRecord        gTheEvent;                    /* the current event   */
  45. int                gHaveEvent;                    /* do we have an event */
  46. long            gSleepTime = 15;            /* time to sleep/WNE   */
  47. int                gEventMask = everyEvent;    /* current event mask  */
  48. RgnHandle        gMouseRgn = NULL;            /* current mouse rgn   */
  49.  
  50.  
  51. void main()
  52. {
  53.     Init();
  54.     Check4Sys7();    /* this exits if Sys 7 isn't running */
  55.     InitAEs();        /* initialize my Apple Event handlers */
  56.     SetupMenus();
  57.     MyPPCInit();
  58.  
  59.     PostWithPPCBrowser();
  60.     while (1) {
  61.         gHaveEvent = WaitNextEvent( gEventMask, &gTheEvent, 
  62.             gSleepTime, gMouseRgn);
  63.         if (gHaveEvent)
  64.             DoEvent();
  65.     } /* while */
  66. } /* main() */
  67.  
  68.  
  69. static void Init()
  70. {
  71.     register int i;
  72.     InitGraf(&qd.thePort);
  73.     InitFonts();
  74.     InitWindows();
  75.     InitMenus();
  76.     TEInit();
  77.     InitDialogs(NULL);
  78.     InitCursor();
  79.     for (i=0; i<3; i++) (void)MoreMasters();    
  80.     FlushEvents(everyEvent, 0);
  81.     SetEventMask(everyEvent);
  82. } /* Init() */
  83.  
  84.  
  85. static void Check4Sys7(void)
  86. {
  87. #define _Gestalt 0xa1ad   /* Gestalt trap number (from IM) */
  88.     if (TrapAvailable(_Gestalt)) {
  89.         long result, mask=0x0700; /* mask = system 7.x */
  90.  
  91.         if ( (Gestalt( gestaltSystemVersion, &result) == noErr) &&
  92.             ( mask == (result & mask)) )
  93.             return;
  94.     }
  95.     /* Let user know we need sys 7, */
  96.     /* if Gestalt isn't on the system, don't even check the others */
  97.     DoAlert("\pThe Application Killer requires",
  98.             "\pSystem 7 in order to work","\pproperly.","\p");
  99.     ExitToShell();
  100.  
  101. } /* Check4Sys7() */
  102.  
  103.  
  104. static void SetupMenus(void)
  105. {
  106.     Handle mbarH;
  107.     
  108.     mbarH = (Handle)GetNewMBar(MENU_BAR);
  109.     if (mbarH == NULL) {
  110.         SysBeep(1); SysBeep(1);
  111.         ExitToShell();
  112.     }
  113.     SetMenuBar( mbarH);
  114.     gAppleM= GetMenu(APPLE_MENU);
  115.     AddResMenu( gAppleM, 'DRVR');
  116.     DrawMenuBar();
  117. } /* SetupMenus() */
  118.  
  119.  
  120. void Cleanup(void)
  121. {
  122.     ExitToShell();
  123. } /* Cleanup() */
  124.  
  125.  
  126. static void DoEvent(void)
  127. {
  128.     switch (gTheEvent.what) {
  129.         case mouseUp:
  130.         case mouseDown:
  131.             DoMouseDown();
  132.             break;
  133.         case keyDown:
  134.         case autoKey:
  135.             DoKeyDown();
  136.             break;
  137.         case activateEvt:
  138.         case updateEvt:
  139.         case osEvt:
  140.             break;
  141.         case kHighLevelEvent:
  142.             DoHLEvent(&gTheEvent);
  143.             break;
  144.     }
  145. } /* DoEvent() */
  146.  
  147.  
  148. static void DoMouseDown(void)
  149. {
  150.     switch (FindWindow( gTheEvent.where, &gWhichWindow)) {
  151.         case inSysWindow:
  152.             SystemClick( &gTheEvent, gWhichWindow);
  153.             break;
  154.         case inMenuBar:
  155.             DoCommand( MenuSelect(gTheEvent.where) );
  156.             break;
  157.         case inDrag:
  158.         case inContent:
  159.         case inGoAway:
  160.             break;
  161.     }
  162. } /* DoMouseDown() */
  163.  
  164.  
  165. static void DoKeyDown(void)
  166. {
  167.     char theChar = (char)(gTheEvent.message & charCodeMask);
  168.     if (gTheEvent.modifiers & cmdKey)
  169.         DoCommand( MenuKey(theChar));
  170. } /* DoKeyDown() */
  171.  
  172.  
  173. static void DoCommand(long mResult)
  174. {
  175.     int theItem = LoWord(mResult);
  176.     int theMenu = HiWord(mResult);
  177.     Str255 name;
  178.     int temp;
  179.     
  180.     switch (theMenu) {
  181.         case APPLE_MENU:
  182.             if (theItem == ABOUT_ITEM) {
  183.                 DoAbout();
  184.             } else {
  185.                 GetItem( gAppleM, theItem, name);
  186.                 temp = OpenDeskAcc(name);
  187.             }
  188.             break;
  189.             
  190.         case FILE_MENU:
  191.             switch (theItem) {
  192.                 case POST_ITEM:
  193.                     PostWithPPCBrowser();
  194.                     break;
  195.                 case LAUNCH_ITEM:
  196.                     DoLau